package com.jack.leetcode.eachproblem_2020;

import java.util.HashMap;
import java.util.Map;

public class 任务调度器 {

    public int leastInterval(char[] tasks, int n) {
        Map<Character, Integer> freqMap = new HashMap<>();
        int maxLen = 0;
        for(char c : tasks){
            Integer orDefault = freqMap.getOrDefault(c, 0);
            freqMap.put(c, orDefault + 1);
            maxLen = Math.max(maxLen, orDefault + 1);
        }

        int maxCount = 0;
        for (Map.Entry<Character, Integer> entry : freqMap.entrySet()) {
            if(entry.getValue() == maxLen){
                maxCount++;
            }
        }
        return Math.max(tasks.length, (maxLen - 1) * (n - 1) + maxCount);
    }
}
